import BaseEvent from '@/components/events/BaseEvent'
import { EventBus } from '@/store/index'
import { getCompIdByParam } from '@/plugins/utils'
import { isHasOwnProperty } from '@/plugins/utils'
import { COMPONENT_TYPES } from '@/common/const';
const rotate3dTime = 5000

export default class Rotate3dEvent extends BaseEvent{
  constructor() {
    super();
  }

  excute({item, event, isPostWbEventOrCommonEvents}){
    this.item = item;
    this.event = event;

    let rotateUrl;
    let randomIndex = -1;
    if (this.item.lists && this.item.lists.length > 0) {
      randomIndex = _.random(0, this.item.lists.length - 1);
      rotateUrl = this.item.lists[randomIndex].url;
    }
    //别的关联组件给这个组件设置rotateIndex
    let isGetOther = event.comps[2].attrs.value;
    let relateCompId = getCompIdByParam(event.comps[0].attrs.value);
    let relateCompData = this.componentMap[relateCompId];
    if(isGetOther){
      if(relateCompId && relateCompData && relateCompData.commonAttr.itemIndex >= 0){
        randomIndex = relateCompData.commonAttr.itemIndex;
      }
    }else{
      //别的组件设置的值
      if(this.item.commonAttr.itemIndex >= 0){
        randomIndex = this.item.commonAttr.itemIndex;
      }
      //给别的组件设置index
      if(relateCompId && relateCompData){
        relateCompData.commonAttr.itemIndex = randomIndex;
      }
    }

    if(randomIndex != -1){
      this.item.commonAttr.itemIndex = randomIndex;
    }

    if(this.item.lists[randomIndex]){
      rotateUrl = this.item.lists[randomIndex].url;
    }

    //event.comps[0]是否播放动画
    let isPlayAnimate = true;
    isPlayAnimate = event.comps[1] ? event.comps[1].attrs.value : true;

    if (rotateUrl) {
      let shareData = { url: rotateUrl }
      this.store.commit('updateShareData', shareData);
      if(!this.commonAttr.isDynamic) {
        this.store.commit("updateComponentAttr", { id: this.item.id, commonAttr: { url: rotateUrl }});
      }
    }

    let rotate3dTime_ = rotate3dTime;
    if(event.comps[3] && event.comps[3].attrs.value){
      let obj = JSON.parse(event.comps[3].attrs.value)
      if(isHasOwnProperty(obj,'delayTime')){
        const delayTime = JSON.parse(event.comps[3].attrs.value).delayTime;
        if(delayTime > 0){
          rotate3dTime_ = delayTime * 1000;
        }
      }
      // {
      //   "rotateGroup": true
      // }
      // {"audio":"https://static.hd.xxx.com/upload/biz/tmp/65376222_2101.mp3","btnUrl":"https://static.hd.xxx.com/upload/biz/1/83938244_2274.png"}
      if(isHasOwnProperty(obj,'rotateGroup')){
        if(this.item.interactionData){
          let element = this.item.interactionData.lists[randomIndex]
          _.merge(element,JSON.parse(element.params))
          this.updateItem(element)
        }
      }
    }
    if(isPlayAnimate){
      window.setTimeout(() => {
        EventBus.$emit("rotate3d", { isPlay: true, id: item.id });
      }, 100)
      item.rotateSetTimeoutCb = window.setTimeout(() => {
        EventBus.$emit("rotate3d", { isPlay: false, id: item.id });
        isPostWbEventOrCommonEvents && isPostWbEventOrCommonEvents({ loadEvent: null, e: null });
        window.clearTimeout(item.rotateSetTimeoutCb);
        item.rotateSetTimeoutCb = null;
      }, rotate3dTime_)
    }else{
      isPostWbEventOrCommonEvents && isPostWbEventOrCommonEvents({ loadEvent: null, e: null });
    }
    return rotateUrl
  }

  updateItem(element){
    _.forEach(this.item.components,(component) =>{
      let id = component.id
      switch(component.cid){
        case COMPONENT_TYPES.wb_audio:
          this.store.commit("updateComponentAttr", { id, commonAttr: { url: element['audio']}});
          break
        case COMPONENT_TYPES.wb_img:
          if(component.events && component.events.link){
            // 判断图片是否绑定了跳转事件
            if(element['jumpImg']){
              this.store.commit("updateComponentAttr", { id, commonAttr: { url: element['jumpImg']}});
            }
            if(element['jumpUrl']){
              let link = component.events.link
              link.comps[0].attrs.value = element['jumpUrl']
            }
          }else{
            this.store.commit("updateComponentAttr", { id, commonAttr: { url: element['url']}});
          }
          break
        case COMPONENT_TYPES.wb_btn:
          break
      }
    });
  }
  destroy(){
    super.destroy();
  }
}
